package com.jgsu.farm.controller;

import com.jgsu.farm.entity.Protection;
import com.jgsu.farm.exception.*;
import com.jgsu.farm.service.ProtectionService;
import com.jgsu.farm.utils.CommonResult;
import com.jgsu.farm.utils.ResultCode;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(value = "farm/sysuser/protection", tags = "植保知识信息管理模块")
@RestController
@RequestMapping("farm/sysuser/protection")
public class ProtectionController {
    @Autowired
    ProtectionService protectionService;

    /**
     * 植保知识增加
     */
    @ApiOperation(value = "植保知识添加功能")
    @PostMapping(value = "/add", produces = "application/json")
    public CommonResult protectionAdd(Protection protection) {
        try {
            protectionService.add(protection);
            log.info("植保知识添加成功，植保知识title为：{}",protection.getTitle());
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataAddException e) {
            log.error("植保知添加异常,title为：{}",protection.getTitle() );
            throw new DataAddException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 植保知识删除
     */
    @ApiOperation(value = "植保知识删除功能")
    @DeleteMapping(value = "/delete", produces = "application/json")
    public CommonResult protectionDelete(@RequestParam(value = "knowid") Integer knowid) {
        try {
            protectionService.delete(knowid);
            log.info("删除id为{}的植保知识信息",knowid);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataNotExistException e) {
            log.error("不存在id为:{}的植保知识信息，删除失败",knowid);
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 植保知识查询
     */
    @ApiOperation(value = "植保知识查询功能")
    @GetMapping(produces = "application/json")
    public CommonResult<PageInfo<Protection>> protectionList(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                             @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pagesize) {
        try {
            PageInfo<Protection> protections = protectionService.selectAll(page, pagesize);
            log.info("查询所有的植保知识信息");
            return CommonResult.success(protections);
        } catch (DataNotExistException e) {
            log.error("植保知识表中没有数据");
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 植保知识修改
     **/
    @ApiOperation(value = "植保知识修改功能")
    @PutMapping(value = "/update", produces = "application/json")
    public CommonResult protectionUpdate(Protection protection) {
        try {
             protectionService.update(protection);
            log.info("更新title为{}的植保知识成功",protection.getTitle());
            return CommonResult.success(protection);
        } catch (DataNotExistException e) {
            log.error("更新title为{}的植保知识失败",protection.getTitle());
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }
}
